using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Ent.Web.Helper;
using Ent.Voice;
public partial class Controls_UserDetails : System.Web.UI.UserControl//Ent.Web.WebControl.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblAboutMe.Text = ResourceHelper.Get("LBL_AboutMe");
        lblDOB.Text = ResourceHelper.Get("LBL_DOB");
        lblEmail.Text = ResourceHelper.Get("LBL_Email");
        lblFName.Text = ResourceHelper.Get("LBL_FName");
        lblHobbies.Text = ResourceHelper.Get("LBL_Hob");
        lblLanguage.Text = ResourceHelper.Get("LBL_Language");
        lblLName.Text = ResourceHelper.Get("LBL_LName");
        lblLocation.Text = ResourceHelper.Get("LBL_Loc");
        lblPassword.Text = ResourceHelper.Get("LBL_Password");
        lblPhone.Text = ResourceHelper.Get("LBL_Phone");
        lblPhoto.Text = ResourceHelper.Get("LBL_Photo");
        lblRePass.Text = ResourceHelper.Get("LBL_RePass");
        lblUsername.Text = ResourceHelper.Get("LBL_UserName");
        rfvPassword.ErrorMessage = string.Format(ResourceHelper.Get("MSG_Mandatory"), ResourceHelper.Get("LBL_Password"));
        rfvRePassword.ErrorMessage = string.Format(ResourceHelper.Get("MSG_Mandatory"), ResourceHelper.Get("LBL_RePass"));
        rfvUsername.ErrorMessage = string.Format(ResourceHelper.Get("MSG_Mandatory"), ResourceHelper.Get("LBL_UserName"));
        rfvFirstName.ErrorMessage = string.Format(ResourceHelper.Get("MSG_Mandatory"), ResourceHelper.Get("LBL_FName"));
        rfvLastName.ErrorMessage = string.Format(ResourceHelper.Get("MSG_Mandatory"), ResourceHelper.Get("LBL_LName"));
        rfvEmail.ErrorMessage = string.Format(ResourceHelper.Get("MSG_Mandatory"), ResourceHelper.Get("LBL_Email"));
        if (!IsPostBack)
        {
            btnSubmit.Attributes.Add("onmouseover", "this.src='Images/dj/goh.gif'");
            btnSubmit.Attributes.Add("onmouseout", "this.src='Images/dj/go.gif'");
            System.Collections.Generic.Dictionary<string, string> lang = ResourceHelper.GetLanguages();
            foreach (string key in lang.Keys)
            {
                ddlLang.Items.Add(new ListItem(key, lang[key]));
            }

            System.Collections.Generic.Dictionary<string, int> dateField = DateHelper.GetCollection(DateField.Date);
            foreach (string key in dateField.Keys)
            {
                ddlDate.Items.Add(new ListItem(key, dateField[key].ToString()));
            }

            dateField = DateHelper.GetCollection(DateField.Month);
            foreach (string key in dateField.Keys)
            {
                ddlMonth.Items.Add(new ListItem(key, dateField[key].ToString()));
            }

            dateField = DateHelper.GetCollection(DateField.Year);
            foreach (string key in dateField.Keys)
            {
                ddlYear.Items.Add(new ListItem(key, dateField[key].ToString()));
            }
        }
    }
    protected void btnGo_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            lblMessage.Visible = false;
            User u = GetUserData();
            Ent.Voice.RegisterResponse res = User.RegisterUser(u);
            switch(res)
            {
                case RegisterResponse.UserCreated:
                    Ent.Web.Mail.Processor p = new Ent.Web.Mail.Processor();
                    Ent.Web.Mail.MailContent cont = Ent.Web.Mail.Collection.GetMailContent("UserCreated");
                    p.To = u.Email;
                    p.Subject = cont.Subject;
                    p.Body = string.Format(cont.Body, ConfigurationManager.AppSettings.Get("BaseUrl") + "/ActivateUser.aspx?id=" + HttpUtility.UrlEncode(u.UserID) + "&pass=" + HttpUtility.UrlEncode(u.Password));
                    p.SendMail();
                    Response.Redirect("Info.aspx?t=3&c=UserCreated", false);
                    break;
                case RegisterResponse.UserIdNotAvailable:
                    lblMessage.Text = Common.GetErrorText(ResourceHelper.Get("MSG_UserIdUnAvailable")) + "<br/><hr/>";
                    lblMessage.Visible = true;
                    return;
                case RegisterResponse.EmailIdInUse:
                    lblMessage.Text = Common.GetErrorText(ResourceHelper.Get("MSG_EMailInUse")) + "<br/><hr/>";
                    lblMessage.Visible = true;
                    return;
                case RegisterResponse.Error:
                default:
                    throw new Exception("Registration Failed.");
            }
        }
        catch (System.FormatException fExc)
        {
            lblMessage.Text = Common.GetErrorText(ResourceHelper.Get("MSG_InvalidDate")) + "<br/><hr/>";
            lblMessage.Visible = true;
        }
        catch (Exception exc)
        {
            Common.Log.Error(exc);
            lblMessage.Text = Common.GetErrorText(ResourceHelper.Get("MSG_Error")) + "<br/><hr/>";
            lblMessage.Visible = true;
        }
    }
    private string UploadFile(string fileName)
    {
        if (fuUser.HasFile)
        {
            HttpPostedFile file = (HttpPostedFile)(fuUser.PostedFile);
            int maxUploadSize = MathFunction.ConvertToInteger(ConfigurationManager.AppSettings["MaxUploadSize"], 2000000);
            if (file.ContentLength <= maxUploadSize)
            {
                string file_ext = System.IO.Path.GetExtension(file.FileName);
                string filePath = HttpRuntime.AppDomainAppPath + "/" + ConfigurationManager.AppSettings.Get("UserImage") + fileName + "." + file_ext;
                fuUser.PostedFile.SaveAs(filePath);
                return fileName + "." + file_ext;
            }
            else
                throw new Exception("Photo file should not be bigger than 500 kb.");
        }
        return "";
    }
    private User GetUserData()
    {
        User retValue = new User();
        retValue.AboutMe = txtAboutMe.Text;
        retValue.DOB = Convert.ToDateTime(ddlMonth.SelectedValue + "/" + ddlDate.SelectedValue + "/" + ddlYear.SelectedValue);
        retValue.Email = txtEmail.Text;
        retValue.FName = txtFName.Text;
        retValue.Hobbies = txtHobbies.Text;
        retValue.Language = ddlLang.SelectedValue;
        retValue.LName = txtLName.Text;
        retValue.Location = txtLocation.Text;
        retValue.Password = txtPass.Text;
        retValue.Phone = txtPhone.Text;
        retValue.PhotoId = UploadFile(txtUsername.Text);
        retValue.UserID = txtUsername.Text;
        return retValue;
    }
}
